package com.etc.servlet.shopcar;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.icss.util.JDBCUtil;


@WebServlet("/shopcar/login3")
public class Login3Servelt extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String uname = req.getParameter("uname");
		String password = req.getParameter("password");
		
		
		
		
		String sql = "\r\n" + 
				"\r\n" + 
				"\r\n" + 
				"SELECT id, uname FROM userinfomst\r\n" + 
				"WHERE uname = ?";
		
		
		try {
			Connection conn = JDBCUtil.getConn();
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, uname);
			ResultSet re = ps.executeQuery();
			User u = null;
			if (re.next()) {
				u = new User();
				u.setUid(re.getInt(1));
				u.setUname(re.getString(2));
			}
			
			if (u != null) {
				req.getSession().setAttribute("uname", uname);
				Gson g = new Gson();
				resp.getWriter().write(g.toJson(u));
				return;
	
			
			} else {
				
			}
			
			conn.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	
}
